﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using MyAlbum.Utility;
using MyAlbum.DataAccess.LinqToSql;


namespace MyAlbum.Data.Linq
{
    public class SectionRepository : ISectionRepository
    {
        private string _connectionString;
        public SectionRepository(string connectionString)
        {
            Assert.IsNotNull(connectionString);
            this._connectionString = connectionString;
        }
        #region IRepository<Section> Members
        public List<MyAlbum.Model.Section> GetItems()
        {
            return GetItems(true);
        }

        public List<MyAlbum.Model.Section> GetItems(bool visible)
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                return ImagesDB.Sections.Where(s => (visible ? s.Visible : true)).Select(s =>
                    new MyAlbum.Model.Section()
                    {
                        ContentUrl = s.ContentUrl,
                        Description = s.Description,
                         TimeStamp = s.TimeStamp,
                        Id = s.Id.ToString(),
                        Name = s.Name,
                        Visible = s.Visible
                    }).ToList();
            }
        }

        public void AddItem(MyAlbum.Model.Section item)
        {
            Assert.IsNotNull(item);
            Assert.IsNotNull(item.Name);
            Section s = new Section()
            {
                  Id=Guid.NewGuid(),
                Visible = item.Visible,
                Name = item.Name,
                Description = item.Description,
                ContentUrl = item.ContentUrl
            };
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                ImagesDB.Sections.InsertOnSubmit(s);
                ImagesDB.SubmitChanges();
            }
            item.Id = s.Id.ToString();
        }

        public void DeleteItem(MyAlbum.Model.Section item)
        {
            Assert.IsNotNull(item);
            
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                var old = ImagesDB.Sections.FirstOrDefault(s => s.Id == new Guid(item.Id));
                if (old != null)
                {
                    ImagesDB.Sections.DeleteOnSubmit(old);
                    ImagesDB.SubmitChanges();
                }
            }
        }

        public void UpdateItem(MyAlbum.Model.Section item)
        {
            Assert.IsNotNull(item);
            Assert.IsNotNull(item.Name);
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                var old = ImagesDB.Sections.FirstOrDefault(s => s.Id ==new Guid( item.Id));
                if (old != null)
                {
                    old.ContentUrl = item.ContentUrl;
                    old.Description = item.Description;
                    old.Name = item.Name;
                    old.Visible = item.Visible;
                    ImagesDB.SubmitChanges();
                }
            }
        }

        #endregion


        #region ISectionRepository Members

        public MyAlbum.Model.Section GetItemById(string sectionId)
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                var item = ImagesDB.Sections.FirstOrDefault(s => s.Id == new Guid(sectionId));
                MyAlbum.Model.Section section = null;
                if (item != null)
                {
                    section = new MyAlbum.Model.Section()
                    {
                        Id = item.Id.ToString(),
                        ContentUrl = item.ContentUrl,
                        Description = item.Description,
                        Name = item.Name,
                        Visible = item.Visible,
                         TimeStamp=item.TimeStamp
                    };
                }
                return section;
            }
        }

        #endregion

        #region ISectionRepository Members


        public int CountItems(bool visible)
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                return ImagesDB.Sections.Count();
            }
        }

        #endregion
    }
}
